/*------------------------------------------------------------------------------
 * PROJ : PrototypeWithSpring
 * NAME : sculove.test.srcParser.KeicSrcParser.java
 *        
 * Copyright 2009 sculove All rights reserved
 *------------------------------------------------------------------------------
*/
package sculove.test.parser.src;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import jxl.CellView;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import sculove.spring.framework.util.SStringUtil;

/**
 * <pre>
 * sculove.test.srcParser.KeicSrcParser.java
 * TODO
 * 
 * </pre>
 * @date : 2009. 10. 19. ���� 5:37:03
 * @version :
 * @author : sculove
 */
public class KeicSrcParser {
	private final Log logger = LogFactory.getLog(this.getClass());
	
	public Map<String, ArrayList<String>> pbcs = new HashMap<String, ArrayList<String>>();
	
	public static void main(String[] args) {
		KeicSrcParser srcParser = new KeicSrcParser();
		File baseDir = new File("E:\\keicDev\\workspace\\keicPfm\\src");
		
		System.out.println("--Start to parse Srcs...");
        if(baseDir.isDirectory()) {
        	srcParser.searchSrc(baseDir);
        }
        System.out.println("--End to parse Srcs...");
        System.out.println("--Start to write excel...");
        srcParser.writeExcel("C:/PBC���.xls");
        System.out.println("--End to write excel...");
	}
	
	
    /**
     * java src ���� ����
     * @param target
     */
    public void searchSrc(File target) {
        if(target.isDirectory()) {    // Directory�� ���
            String[] child = target.list();
            for(int i = 0; i < child.length; i++){
            	searchSrc(new File( target.getAbsolutePath() + "\\" + child[i]));
            }
        } else {    // File�� ���
        	if("java".equalsIgnoreCase(SStringUtil.unqualify(target.getAbsolutePath(), '.')) 
                    && (SStringUtil.unqualify(target.getAbsolutePath(), '/').endsWith("PBC.java"))
                    && (target.getName().startsWith("I"))
                    && (Character.isUpperCase(target.getName().charAt(1)))
            ) {
        		ArrayList<String> operations = parsingFile(target);
            	pbcs.put(target.getAbsolutePath(), operations);
            }
        }
    }
    
    /**
     * 
     * @return
     * @throws Exception
     */
    public ArrayList<String> parsingFile(File file) {
    	ArrayList<String> list = new ArrayList<String>();
    	
    	BufferedReader br = null;
    	
    	try {
        	br = new BufferedReader(new FileReader(file));
        	
        	String lineContent;
        	while ( (lineContent = br.readLine()) != null ) {
        		int idx = -1;
        		if( (idx = lineContent.indexOf("public abstract void ")) != -1 ) {
        			list.add(lineContent.substring(21 + idx, lineContent.indexOf("(")));
        		} else if( (idx = lineContent.indexOf("public void ")) != -1 ) {
       	    		list.add(lineContent.substring(12 + idx, lineContent.indexOf("(")));
        		}
        	}			
		} catch (Exception e) {
			logger.error("�� ERROR �� - " + file.getAbsolutePath());
		} finally {
			try { if(br != null) { br.close(); br = null; } } catch(Exception be) {}
		}
    	return list;
    }
    
    /**
     * Excel �����
     * @param filepath
     */
    private void writeExcel(String filepath) {
        WritableWorkbook workbook = null;
        WritableSheet sheet = null;
        File excelFile = new File(filepath);
        
        try{
            workbook = Workbook.createWorkbook(excelFile);
            sheet = workbook.createSheet("PBC���-����", 0);
            
            // �÷� Auto size ����
            CellView autoSizeCellView = new CellView();
            autoSizeCellView.setAutosize(true);
            sheet.setColumnView(0, autoSizeCellView);
            sheet.setColumnView(1, autoSizeCellView);
            sheet.setColumnView(2, autoSizeCellView);
            sheet.setColumnView(3, autoSizeCellView);
            
            sheet.addCell(new Label(0,0,"������"));
            sheet.addCell(new Label(1,0,"Application"));
            sheet.addCell(new Label(2,0,"PBC"));
            sheet.addCell(new Label(3,0,"Operaion"));

            
            int row = 1;
            Iterator<String> iter = pbcs.keySet().iterator();
            while(iter.hasNext()) {
                String key = iter.next();
                
                ArrayList<String> list = pbcs.get(key);
                for(int i=0; i<list.size(); i++) {
                    String appCd = key.substring(key.indexOf("src\\keic\\")+9,key.indexOf("src\\keic\\")+12);
                    String appNm ="";
                    if(appCd.equals("Keic")) appNm="����"; 
                    else if(appCd.equals("itc")) appNm="ITSM";
                    else if(appCd.equals("mac")) appNm="ȸ��";
                    else if(appCd.equals("mhc")) appNm="�λ�";
                    else if(appCd.equals("muc")) appNm="��������";
                    else if(appCd.equals("rac")) appNm="�μ�";
                    else if(appCd.equals("rcc")) appNm="����";
                    else if(appCd.equals("rec")) appNm="���̹����⺸����";
                    else if(appCd.equals("ric")) appNm="�ſ�����";
                    else if(appCd.equals("rmc")) appNm="��ǰ";
                    else if(appCd.equals("rrc")) appNm="���İ�";
                    else if(appCd.equals("rsc")) appNm="�?����";
                    else if(appCd.equals("ruc")) appNm="��������";
                    else if(appCd.equals("zzz")) appNm="�������";
                	
                	String opertaion = (String)list.get(i);
                	sheet.addCell(new Label(0,row, appNm));
                    sheet.addCell(new Label(1,row, appCd));
                    sheet.addCell(new Label(2,row, SStringUtil.unqualify(key, '\\').substring(0, SStringUtil.unqualify(key, '\\').indexOf("."))));
                    sheet.addCell(new Label(3,row, opertaion));
                    row++;
                }
            }
        }catch(Exception e){
           e.printStackTrace();
        } finally {
            try{
                if(workbook != null) workbook.write();
                if(workbook != null) workbook.close();   
            }catch(Exception e2){}
        }
        
    }    
}
